import { YForm, YInterception, YItemConfig, YItemRow } from '../interface';
import ItemRowList from './ItemRowList';

type FormConfig = Array<
  YItemConfig | YItemRow | YItemRow[] | string | number | boolean
>;

export default class FormUtil {
  config: FormConfig;
  itemRowList: ItemRowList;

  constructor(config: FormConfig = [], public form: YForm) {
    this.config = config;
    this.itemRowList = this.getItemRowLit(config);
  }

  getItemRowLit(formConfig: FormConfig) {
    const rowList = new ItemRowList(null, null, this.form);
    formConfig.forEach((config) => rowList.add(config));
    return rowList;
  }

  getFormConfig(interception?: YInterception) {
    return this.itemRowList.getConfig(interception);
  }

  setDisabled(disable: boolean, props?: string | string[]) {
    props = props || [];
    if (typeof props === 'string') {
      props = [props];
    }
    this.itemRowList.setDisabled(disable, props);
    return this;
  }

  setRequired(required: boolean, props?: string | string[]) {
    if (typeof props === 'string') {
      props = [props];
    }
    this.itemRowList.setRequired(required, props);
    return this;
  }

  setOptions(prop: string, options: { [key: string]: any }[]) {
    this.itemRowList.setOptions(prop, options);
  }

  setBind(props: string | string[], key: string, value: any) {
    this.itemRowList.setBind(props, key, value);
  }

  setOptionsMap(options: { [key: string]: { [key: string]: any }[] }) {
    Object.keys(options).forEach((key) => {
      this.setOptions(key, options[key]);
    });
    return this;
  }

  setConfig(prop: string, config: YItemConfig) {
    this.itemRowList.setConfig(prop, config);
  }

  removeConfig(props: string | string[]) {
    this.itemRowList.removeConfig(props);
  }

  appendConfigBefore(prop: string, config: YItemConfig) {
    this.itemRowList.appendConfigBefore(prop, config);
  }
  appendConfigAfter(prop: string, config: YItemConfig) {
    this.itemRowList.appendConfigAfter(prop, config);
  }

  replaceConfig(prop: string, config: YItemConfig) {
    this.itemRowList.replaceConfig(prop, config);
  }

  removeRow(id: string | number) {
    this.itemRowList.removeRow(id);
  }
  appendRow(config: YItemConfig | YItemRow | YItemRow[]) {
    this.itemRowList.add(config);
  }

  appendRowBefore(
    id: string | number,
    config: YItemConfig | YItemRow | YItemRow[]
  ) {
    this.itemRowList.appendRowBefore(id, config);
  }
  appendRowAfter(
    id: string | number,
    config: YItemConfig | YItemRow | YItemRow[]
  ) {
    this.itemRowList.appendRowAfter(id, config);
  }

  getItemConfigs() {
    return this.itemRowList.getItemConfigs().filter((config) => !!config);
  }

  getRows() {
    return this.itemRowList.getRows();
  }

  getEvents(prop: string) {
    const itemConfigArray = this.getItemConfigs();
    const itemConfig = itemConfigArray.find(
      (itemConfig) => itemConfig.prop === prop
    );
    if (itemConfig) {
      return itemConfig.events || null;
    }
    return null;
  }
}
